<?php

 /**
  * @file This file handles security measures and should be called at the top of all other files.  This file also makes the database connection.
  * 
  */

require_once("config.php");
require("eve.php");

// Require eve IGB with trust turned on 
$eve = new eveBrowser($claimsUrl);
$eve->getTrust();

$conn = mysql_connect($mysqlHost, $mysqlUser, $mysqlPassword);
mysql_select_db($mysqlDb);

// Collect player ID information if available and creates the user if they do not exist
if (isset($_SERVER['HTTP_EVE_CHARNAME']))
{
	$query = sprintf("select * from players where charname = '%s'", mysql_real_escape_string($_SERVER['HTTP_EVE_CHARNAME']));
	$res = mysql_query($query, $conn);	

	if (mysql_num_rows($res) == 0)
	{
		$query = sprintf("insert into players (charname, charid, clone) values ('%s', %d, 'Alpha');", mysql_real_escape_string($_SERVER['HTTP_EVE_CHARNAME']), mysql_real_escape_string($_SERVER['HTTP_EVE_CHARID']));
		mysql_query($query, $conn);		
	}
	else
	{
		// Update any information that may need updating here
		$query = sprintf("update players set charid = %d where charname = '%s'", mysql_real_escape_string($_SERVER['HTTP_EVE_CHARID']), mysql_real_escape_string($_SERVER['HTTP_EVE_CHARNAME']));
		mysql_query($query, $conn);
	}

	// Create player information global variable
	$query = sprintf("select * from players where charname = '%s'", mysql_real_escape_string($_SERVER['HTTP_EVE_CHARNAME']));
	$res = mysql_query($query, $conn);	
	$UserData = mysql_fetch_assoc($res);
}

?>
